<template>
  <el-card shadow="never" header="操作记录">
    <el-container>
      <el-header class="no-flex" height="auto">
    <el-form ref="formRef" :model="data" inline label-width="70px">
      <el-form-item label="模块名称" prop="title" >
        <el-input placeholder="请输入模块名称" clearable  v-model="data.title" style="width: 200px" />
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType" >
        <el-select placeholder="请选择业务类型" clearable v-model="data.businessType" style="width: 200px">
          <el-option v-for="item in dicts.system_operator_type"
                     :key="item.v"
                     :label="item.k"
                     :value="item.v"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="执行状态" prop="status" >
        <el-select placeholder="请选择执行状态" clearable v-model="data.status" style="width: 200px">
          <el-option v-for="item in dicts.system_global_expstatus"
                     :key="item.v"
                     :label="item.k"
                     :value="item.v"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间" prop="date" >
        <el-date-picker style="width: 200px"
                        v-model="data.date"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始"
                        end-placeholder="结束"
                        clearable
                        value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item >
        <el-button type="primary" @click="search" icon="el-icon-search">搜索</el-button>
        <el-button  @click="reset" icon="el-icon-refreshRight">重置</el-button>
      </el-form-item>
      <el-form-item style="display: flex; margin-bottom: 0">
        <el-button type="danger" @click="deleteRow" icon="el-icon-delete" :disabled="ids.length === 0">删除</el-button>
        <el-button type="danger" @click="empty" icon="el-icon-refresh">清空</el-button>
      </el-form-item>
    </el-form>
      </el-header>
      <el-main class="nopadding">
    <el-table @selection-change="handleSelectionChange" :selection="ids" :data="data" height="auto" border
              paginationLayout="total, prev, pager, next" hideDo>
      <el-table-column align="center" type="selection"/>
      <el-table-column label="模块名称" prop="title" align="center" width="100px"/>
      <el-table-column label="业务类型" prop="businessType" align="center"/>
      <el-table-column label="操作类别" prop="operatorType" align="center"/>
      <el-table-column label="请求地址" prop="operUrl" width="200px"/>
      <el-table-column label="请求方式" prop="requestMethod" align="center"/>
      <el-table-column label="主机地址" prop="operIp" width="100px"/>
      <el-table-column label="操作地点" prop="operLocation" align="center"/>
      <el-table-column label="状态" align="center">
        <template #default="{ row }">
          <el-tag v-if="row.status == 0 ">正常</el-tag>
          <el-tag v-if="row.status == 1 ">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operTime" label="操作时间" width="160px"/>
      <el-table-column label="操作" align="center" width="80">
        <template #default="{ row }">
          <div class="sys-table-main-actions">
            <el-link icon="el-icon-view" type="primary" @click="openDialog(row)">详情</el-link>
          </div>
        </template>
      </el-table-column>
    </el-table>
      </el-main>
    <el-footer style="display: flex;justify-content: flex-end">
    <el-pagination
      :current-page="current"
      :page-size="size"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-footer>
    </el-container>
  </el-card>

  <el-dialog
    title="日志详情"
    width="800px"
    v-model="visible"
    :before-close="closeDialog"
  >
    <el-descriptions :column="2" class="el-descriptions">
      <el-descriptions-item label="操作模块">{{ logger.title }}</el-descriptions-item>
      <el-descriptions-item label="请求地址">{{ logger.operUrl }}</el-descriptions-item>

      <el-descriptions-item label="操作信息">{{ logger.operName }} / {{ logger.operIp }} / {{
          logger.operLocation
        }}
      </el-descriptions-item>
      <el-descriptions-item label="请求方式">{{ logger.requestMethod }}</el-descriptions-item>
    </el-descriptions>

    <el-descriptions :column="2" class="el-descriptions">
      <el-descriptions-item label="请求参数"></el-descriptions-item>
    </el-descriptions>
    <div>
      <pre><code class="language-json">{{ logger.operParam ? logger.operParam.trim() : logger.operParam }}</code></pre>
    </div>

    <el-descriptions :column="2" class="el-descriptions">
      <el-descriptions-item label="操作状态">{{ logger.status }}</el-descriptions-item>

      <el-descriptions-item label="操作时间">{{ logger.operTime }}</el-descriptions-item>
    </el-descriptions>

    <el-descriptions :column="1">
      <el-descriptions-item label="响应数据"></el-descriptions-item>
    </el-descriptions>
    <div>
      <pre><code class="language-json">{{ logger.jsonResult }}</code></pre>
    </div>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import Prism from 'prismjs';
import 'prism-themes/themes/prism-atom-dark.css';

export default {
  name: 'log',
  dicts:['system_global_expstatus','system_operator_type'],
  data() {
    return {
      checked: false,
      ids: [],//选中数组
      data: [],
      pages: 1,
      size: 10, //分页大小
      current: 1, //当前页
      total: 0, //一共几条
      visible: false,
      logger: {}
    }
  },
  mounted() {
    this.getLoglist()
  },
  methods: {
    async getLoglist() {
      const {data} = await this.$API.auth.logger.list.get({size: this.size, current: this.current})
      this.current = data.current
      this.size = data.size
      this.data = data.records
      this.total = data.total
      this.pages = data.pages
    },
    handleSizeChange(val) {
      this.size = val
      this.getLoglist()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getLoglist()
    },
    // 搜索
    async search() {
      const {data} = await this.$API.auth.logger.list.get({
        size: this.size,
        current: this.current,
        title: this.data.title,
        businessType: this.data.businessType,
        status: this.data.status,
        date: this.data.date,
      });
      this.data = data.records;
      this.total = data.total;
    },
    //重置
    reset() {
      this.$refs.formRef.resetFields();
      this.getLoglist()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
    },
    //删除
    deleteRow() {
      this.$confirm('此操作将删除日志,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonClass: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$API.auth.logger.delete.post(this.ids)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.getLoglist()
      })

    },
    //清空
    empty() {
      this.$confirm('此操作将清空日志,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonClass: '取消',
        type: 'warning'
      }).then(async () => {
            await this.$API.auth.logger.empty.get()
            this.$message({
              type: 'success',
              message: '清空成功'
            })
            this.getLoglist()

      })
    },
    async openDialog(row) {
      let res = await this.$API.auth.logger.detail.get(row.id)
      this.logger = res.data
      this.visible = true
      this.$nextTick(() => {
        Prism.highlightAll()
      })
    },
    closeDialog() {
      this.logger = {}
      this.visible = false
    }
  }
}
</script>

<style>
.el-descriptions .el-descriptions__cell {
  width: 50%;
}
pre {
  padding: 1em !important;
  white-space: pre-wrap;
}
</style>
<style lang="scss" scoped>
.inline {
  display: inline-flex;
  vertical-align: middle;
  width: 30%;
}
</style>
